import webconst from '@/utils/webconst.js'

import {
	getToken
} from '@/utils/usertoken.js'






//上传微信图片
export const uploadWxImage = (img) => {
	return new Promise((resolve, reject) => {

		const tempFilePath = img; // 小程序临时头像路径
		const path = webconst.base_url + "/upload/singleimage";
		if (!tempFilePath) return
		var token = getToken();
		uni.uploadFile({
			url: path,
			filePath: tempFilePath,
			name: 'file', // 后端接收参数名
			header: {
				'Authorization': 'Bearer ' + token // 可选：携带 token
			},
			success: (res) => {
				const data = JSON.parse(res.data)
				if (data.code==0) {
					resolve(data.data);
					uni.showToast({
						title: '上传成功'
					})
				} else {
					uni.showToast({
						title: '上传失败',
						icon: 'none'
					})
				}
			},
			fail: (err) => {
				console.error('上传失败', err)
				uni.showToast({
					title: '网络错误',
					icon: 'none'
				})
			}
		})
	})
}

//无赖只能把这玩意透出来
export default uploadWxImage